package com.rpframework.website.device.act.api;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.rpframework.core.BaseAct;
import com.rpframework.core.utils.DictionarySettingUtils;
import com.rpframework.core.utils.MessageFormatter;
import com.rpframework.core.utils.TagUtils;
import com.rpframework.module.common.domain.City;
import com.rpframework.module.common.service.CityService;
import com.rpframework.module.common.service.SMSService;
import com.rpframework.utils.AlgorithmEnum;
import com.rpframework.utils.AlgorithmUtils;
import com.rpframework.utils.GenerateUtils;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.domain.Address;
import com.rpframework.website.device.domain.Balance;
import com.rpframework.website.device.domain.Coupon;
import com.rpframework.website.device.domain.Estate;
import com.rpframework.website.device.domain.PayMent;
import com.rpframework.website.device.domain.Room;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.AddressService;
import com.rpframework.website.device.service.BalanceService;
import com.rpframework.website.device.service.CouponService;
import com.rpframework.website.device.service.EstateService;
import com.rpframework.website.device.service.PayMentService;
import com.rpframework.website.device.service.RoomService;
import com.rpframework.website.device.service.UserService;

@Controller
@RequestMapping("/api/user")
public class UserApiAct extends BaseAct{
	Gson gson = new Gson();
	@Resource UserService userService;
	@Resource SMSService smsService;
	@Resource CouponService couponService;
	@Resource RoomService roomService;
	@Resource EstateService estateService;
	@Resource AddressService addressService;
	@Resource CityService cityService;
	@Resource BalanceService balanceService;
	@Resource PayMentService payMentService;
	/**
	 * 注册发送验证码
	 * @param userPhone
	 * @param callback
	 * @return
	 */
	@RequestMapping(value="/sendsms_regist" , produces = "application/json; charset=utf-8")
	public @ResponseBody String sendSMSForRegist(HttpServletRequest request,
			@RequestParam(value = "callback",required = false ) String callback) {
		String tel = request.getParameter("tel");
		if(StringUtils.isEmpty(tel)) {
			throw new APICodeException(-1, "非法手机号!");
		}
		User user = userService.findUserByContact(tel);
		if(user !=null){
			throw new APICodeException(-2, "手机号已注册!");
		}
		String verifyCode = String.valueOf(NumberUtils.random(6));
		String sendContent = DictionarySettingUtils.getParameterValue("sendsms.regist");
		if(StringUtils.isBlank(sendContent)) {
			sendContent =  "本次注册验证码:{}，请牢记";
		}
		sendContent = MessageFormatter.format(sendContent, verifyCode);
		
		// 发送短信验证码
		JsonObject json = new JsonObject();
		boolean flag = smsService.sendSMS(1, tel, verifyCode, sendContent);
		String msg = "注册验证码发送";
		json =  isSuccessJson(flag,msg);
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}

	/**
	 * 注册页面
	 * @param request
	 * @param callback
	 * @param attr
	 * @return
	 * @time 2016年3月7日 下午3:05:56
	 */
	@RequestMapping(value="/regist" , produces = "application/json; charset=utf-8")
	public @ResponseBody String regist(
			HttpServletRequest request,
			@RequestParam(value = "callback",required = false ) String callback,
			RedirectAttributes attr) {
		// 获得请求参数值
		String tel = request.getParameter("tel");
		String name = request.getParameter("name");
		String passWord = request.getParameter("password");
		String code = request.getParameter("code");
		String roomId = request.getParameter("roomId");
		
		JsonObject json = new JsonObject();
		User user = userService.findUserByContact(tel);
		if(StringUtils.isBlank(tel) ||StringUtils.isBlank(passWord) ||StringUtils.isBlank(name) ||StringUtils.isBlank(code) || StringUtils.isBlank(roomId)) {
			json.addProperty("msg", "非法参数");
			json.addProperty("succ", false);
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}
		if(user != null) {
			json.addProperty("msg", "手机号码已经存在");
			json.addProperty("succ", false);
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}
		if(!smsService.checkVerifyCode(1, tel, code)) {
			json.addProperty("msg", "验证码不正确");
			json.addProperty("succ", false);
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}
		Room r = roomService.select(Integer.valueOf(roomId));
		if(r==null){
			json.addProperty("msg", "房间无效");
			json.addProperty("succ", false);
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}
		// 密码MD5加密
		passWord = AlgorithmUtils.encodePassword(passWord, AlgorithmEnum.MD5);
		smsService.setVerifyCodeVaild(1, tel); // 设置短信的类型
		user = new User();
		user.setRoomId(Integer.valueOf(roomId));
		user.setTel(tel);
		user.setRealName(name);
		user.setPassword(passWord);
		user.setUserName(tel);
		user.setHeadImg("/device/user/topxiao.png");
		//房间所属小区
		user.setEstateId(r.getRelativeId());
		user.setState(1);//
		user.setCreateTime(System.currentTimeMillis()/1000);
		
		user.setNickName(name);
		user.setDbNo(GenerateUtils.generateCharAndNumr(7));
		user.setSex(2);//默认女
		
		boolean flag = userService.insert(user);
		String msg = "用户注册";
		json =  isSuccessJson(flag,msg);
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	

	/**
	 * 忘记密码
	 * @time 2015年8月10日 上午9:30:39
	 */
	@RequestMapping(value="sendsms_forget_password" , produces = "application/json; charset=utf-8")
	public @ResponseBody String sendSMSForForgetPassowrd(
			HttpServletRequest request,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session) {
		String tel = request.getParameter("tel");
		if(StringUtils.isBlank(tel)) {
			throw new APICodeException(-1, "非法参数!");
		}
		String verifyCode = String.valueOf(NumberUtils.random(6));
		String sendContent = DictionarySettingUtils.getParameterValue("sendsms.forget_passowrd");
		if(StringUtils.isBlank(sendContent)) {
			sendContent =  "本次重置密码验证码:{}，请牢记";
		}
		sendContent = MessageFormatter.format(sendContent, verifyCode);
		boolean flag = smsService.sendSMS(2, tel, verifyCode, sendContent);
		JsonObject json = new JsonObject();
		String msg = "重置密码验证码发送";
		json =  isSuccessJson(flag,msg);
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	/**
	 * 忘记密码重置密码
	 * @param request
	 * @param callback
	 * @return
	 */
	@RequestMapping(value="reset_password" , produces = "application/json; charset=utf-8")
	public @ResponseBody String resetPassword(HttpServletRequest request,
			@RequestParam(value = "callback",required = false ) String callback
			) {
		String tel = request.getParameter("tel");
		String passWord = request.getParameter("password");
		String code = request.getParameter("code");
		JsonObject json = new JsonObject();
		if(StringUtils.isBlank(tel) || StringUtils.isBlank(passWord) || StringUtils.isBlank(code)) {
			throw new APICodeException(-1, "非法参数!");
		}
		if(!smsService.checkVerifyCode(2, tel, code)) {
			throw new APICodeException(-3, "验证码不正确!");
		}
		User user = userService.findUserByContact(tel);
		Assert.notNull(user, "找不到用户!");
		passWord = AlgorithmUtils.encodePassword(passWord, AlgorithmEnum.MD5);
		user.setPassword(passWord);
		smsService.setVerifyCodeVaild(1, tel);
		user.setModifyTime(System.currentTimeMillis()/1000);
		boolean flag = userService.update(user);
		String msg = "重置密码";
		json =  isSuccessJson(flag,msg);
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	
	
	/**
	 * 修改手机号
	 * @time 2015年8月10日 上午9:30:39
	 */
	@RequestMapping(value="/sendsms_modify_tel" , produces = "application/json; charset=utf-8")
	public @ResponseBody String sendSms_Modify_Tel(
			@RequestParam(value = "callback",required = false ) String callback,
			@RequestParam(value = "newTel",required = false ) String newTel,
			HttpSession session) {
		String verifyCode = String.valueOf(NumberUtils.random(6));
		String sendContent = DictionarySettingUtils.getParameterValue("sendsms.forget_passowrd");
		if(StringUtils.isBlank(sendContent)) {
			sendContent =  "本次修改手机号验证码:{}，请牢记";
		}
		sendContent = MessageFormatter.format(sendContent, verifyCode);
		boolean flag = smsService.sendSMS(3, newTel, verifyCode, sendContent);//直接给新手机号发验证码
		JsonObject json = new JsonObject();
		String msg = "修改手机号发送验证码";
		json =  isSuccessJson(flag,msg);
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	/**
	 * 操作结果为boolean类型的返回方法
	 * @param flag
	 * @param msg
	 * @return
	 */
	public JsonObject isSuccessJson(boolean flag, String msg) {
		JsonObject json = new JsonObject();
		Integer code = 500;
		if(flag)code =200;
		json.addProperty("code", code);
		json.addProperty("msg", msg+flag);
		return json ;
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 修改手机号
	 * @param session
	 * @param request
	 * @param attr
	 * @return
	 */
	@RequestMapping("/modify_tel")
	public @ResponseBody String Modify_Tel(HttpSession session, 
			@RequestParam(value = "callback",required = false ) String callback,
			HttpServletRequest request,RedirectAttributes attr) {
		// 获得请求参数值
		User sessionUser = (User) session.getAttribute(SESSION_USER_KEY);
		String tel = request.getParameter("tel");
		String code = request.getParameter("code");
		JsonObject json = new JsonObject();
		if(StringUtils.isBlank(tel) ||StringUtils.isBlank(code)) {
			json.addProperty("msg", "非法参数");
			json.addProperty("succ", false);
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}
		if(!smsService.checkVerifyCode(3, tel, code)) {
			json.addProperty("msg", "验证码不正确"); 
			json.addProperty("succ", false);
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}
		User user = userService.findUserByContact(tel);
		User dbuser = userService.findUserByContact(sessionUser.getTel());
		if(user != null) {
			json.addProperty("msg", "该号码已经被使用");
			json.addProperty("succ", false);
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}
		dbuser.setTel(tel);
		dbuser.setModifyTime(System.currentTimeMillis()/1000);
		boolean flag = userService.update(dbuser);
		if(flag){
			json.addProperty("succ", true);
			json.addProperty("msg", "更改成功");
		}else{
			json.addProperty("succ", false);
			json.addProperty("msg", "更改失败");
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
		
	/**
	 * 修改密码		
	 */
	@RequestMapping(value="/change_pwd" ,produces = "application/json; charset=utf-8")
	public @ResponseBody String changePwd(
			@RequestParam(required=false) String oldPassWord ,
			@RequestParam(required=false) String nPassWord ,
			@RequestParam(required=false) String qPassWord , 
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		JsonObject json = new JsonObject();
		User user = getSessionUser(session);
		if( user == null ){
			throw new APICodeException(-4, "你还没登陆!");
		}
		oldPassWord = AlgorithmUtils.encodePassword(oldPassWord, AlgorithmEnum.MD5);
		if(!oldPassWord.equals(user.getPassword())){
			throw new APICodeException(-1, "原密码错误!");
		}
		if(StringUtils.isBlank(nPassWord)){
			throw new APICodeException(-2, "新密码不能为空!");
		}
		if(!nPassWord.equals(qPassWord)){
			throw new APICodeException(-3, "两次密码不一致!");
		}
		String nPwd = AlgorithmUtils.encodePassword(nPassWord, AlgorithmEnum.MD5);
		user.setPassword(nPwd);
		boolean flag = userService.update(user);
		if(flag){
			json.addProperty("succ", true);
		} else {
			json.addProperty("succ", false);
		}	
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	
	/**
	 * 我的页面 余额 积分 优惠券张数
	 * 昵称 豆包号  所在小区
	 */
	@RequestMapping(value="/userData" ,produces = "application/json; charset=utf-8")
	public @ResponseBody String userData(
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		JsonObject json = new JsonObject();
		User user = getSessionUser(session);
		if( user == null ){
			throw new APICodeException(-4, "你还没登陆!");
		}
		json.addProperty("headImg", TagUtils.getFileFullPath(user.getHeadImg()));
		json.addProperty("nickName", user.getNickName());
		Estate e = estateService.select(user.getEstateId());
		if(e!=null)
			json.addProperty("villageName",e.getName());
		else
		json.addProperty("villageName","我的小区");
		json.addProperty("balance",user.getBalance());//余额
		json.addProperty("point",user.getPoint());//积分 订单每成功一个，积分就累加到积分上去
		String nowTime = TagUtils.formatDate(System.currentTimeMillis()/1000);
		List<Coupon> list = couponService.searchList(user.getId(), nowTime);
		if(list!=null){
			json.addProperty("coupon", list.size());//优惠券的张数
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	/**
	 * 用户退出
	 * @time 2015年7月20日 下午7:21:33
	 */
	@RequestMapping("remove_session_user")
	public @ResponseBody String removeSessionUser(HttpSession session,
			@RequestParam(value = "callback",required = false ) String callback
			){
		JsonObject json = new JsonObject();
		
		User currUser = getSessionUser(session);
		if(currUser != null){
			session.removeAttribute(SESSION_USER_KEY);
		}
		json.addProperty("succ", true);
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	
	/* 修改小区 */
	@RequestMapping(value="/change_estate" ,produces = "application/json; charset=utf-8")
	public @ResponseBody String changeEstate(
			@RequestParam(value = "callback",required = false ) String callback,
			@RequestParam(value = "roomId",required = false ) Integer roomId,
			HttpSession session){
		JsonObject json = new JsonObject();
		User user = getSessionUser(session);
		if( user == null ){
			throw new APICodeException(-4, "你还没登陆!");
		}
		user.setRoomId(roomId);
		boolean flag = userService.update(user);
		String msg = "修改小区";
		json = isSuccessJson(flag, msg);
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	
	/* 用户添加/修改地址   */
	/**
	 * 
	 * @param callback
	 * @param villageId 小区id
	 * @param tel 电话
	 * @param address 小区地址
	 * @param name 收货人
	 * @param id 仅修改时传入
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/change_address" ,produces = "application/json; charset=utf-8")
	public @ResponseBody String change_address(
			@RequestParam(value = "callback",required = false ) String callback,
			@RequestParam(value = "villageId",required = false ) Integer villageId,
			@RequestParam(value = "tel",required = false ) String tel,
			@RequestParam(value = "address",required = false ) String address,
			@RequestParam(value = "name",required = false ) String name,
			@RequestParam(value = "id",required = false ) Integer id,
			HttpSession session){
		JsonObject json = new JsonObject();
		User user = getSessionUser(session);
		if( user == null ){
			throw new APICodeException(-4, "你还没登陆!");
		}
		boolean flag =false;
		if(StringUtils.isBlank(name)||StringUtils.isBlank(address)||StringUtils.isBlank(tel)||NumberUtils.isNotValid(villageId))
			throw new APICodeException(-6, "参数列表错误!");
		String msg = "添加小区";
		if(NumberUtils.isValid(id)){
			msg ="修改小区";
			Address db = addressService.select(id);
			if(db !=null){
				db.setAddress(address);
				db.setTel(tel);
				db.setEstateId(villageId);
				Estate estate = estateService.select(villageId);
				db.setVillageName(estate.getName());
				db.setModifyTime(System.currentTimeMillis()/1000);
				flag = addressService.update(db);
			}else{
				throw new APICodeException(-9, "小区不存在!");
			}
		}else{
			Address t = new Address();
			t.setAddress(address);
			t.setTel(tel);
			t.setUserId(user.getId());
			t.setEstateId(villageId);//小区id
			t.setCreateTime(System.currentTimeMillis()/1000);
			t.setName(user.getRealName());//收货人
			Estate estate = estateService.select(villageId);
			t.setVillageName(estate.getName());
			flag = addressService.insert(t);
		}
		json = isSuccessJson(flag, msg);
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	/* 用户地址列表   */
	@RequestMapping(value="/addressList",produces = "application/json; charset=utf-8")
	public @ResponseBody String list(@RequestParam(value="pager",required = false ) Pager<Address> pager,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		if(pager==null){
			pager=new Pager<Address>();
		}
		User sessionuser=getSessionUser(session);
		if(sessionuser==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		pager.getSearchMap().put("userId",sessionuser.getId().toString());
		pager = addressService.getPager(pager);
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<Address> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(Address t : list){
					JsonObject obj = new JsonObject();
					obj.addProperty("id", t.getId());
					obj.addProperty("name", t.getName());
					obj.addProperty("tel", t.getTel());
					obj.addProperty("address", t.getAddress());
					obj.addProperty("villageId", t.getEstateId());
					Estate estate = estateService.select(t.getEstateId());
					if(estate !=null)
					obj.addProperty("estate", estate.getName());
					array.add(obj);
			}
			json.add("arrays", array);
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}	
	
	
	/**
	 * 我的个人资料
	 */
	@RequestMapping(value="/dataInfo" ,produces = "application/json; charset=utf-8")
	public @ResponseBody String dataInfo(
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		JsonObject json = new JsonObject();
		User sessionuser = getSessionUser(session);
		User user = userService.select(sessionuser.getId());
		if( user == null ){
			throw new APICodeException(-4, "你还没登陆!");
		}
		json.addProperty("headImg", TagUtils.getFileFullPath(user.getHeadImg()));
		json.addProperty("nickName", user.getNickName());
		json.addProperty("dbNo",user.getDbNo());//豆包号
		Estate e = estateService.select(user.getEstateId());
		if(e!=null)
			json.addProperty("villageName",e.getName());//小区名称
		else
		json.addProperty("villageName","我的小区");
		json.addProperty("sign",user.getSign());//余额
		json.addProperty("sex",user.getSex());//性别
		json.addProperty("job",user.getJob());//职位
		json.addProperty("mrriage",user.getMarriage());//情感状态 1未婚 2已婚
		json.addProperty("constellation",user.getConstellation());//星座
		json.addProperty("hobbies",user.getHobbies());//爱好
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	/* 修改个人资料   */
	
	@RequestMapping(value="/update",produces = "application/json; charset=utf-8")
	public @ResponseBody String updateData(
			@RequestParam(value="sex",required = false ) Integer sex,//页面请求参数性别
			@RequestParam(value="sign",required = false ) String sign,//个人签名
			@RequestParam(value="nickName",required = false ) String nickName,//昵称
			@RequestParam(value="headImg",required = false ) String headImg,//头像地址
			@RequestParam(value="job",required = false ) String job,//职位 
			@RequestParam(value="hobbies",required = false ) String hobbies,//兴趣
			@RequestParam(value="constellation",required = false ) String constellation,//星座
			@RequestParam(value="marriage",required = false ) Integer marriage,//情感状态  1未婚 2已婚
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		User sessionuser=getSessionUser(session);
		if(sessionuser==null)throw new APICodeException(-4, "你还没登陆!");
		User user = userService.select(sessionuser.getId());//根据id查询用户返回用户信息
		if(NumberUtils.isValid(sex))user.setSex(sex);//修改用户性别
		if(StringUtils.isNotBlank(nickName))user.setNickName(nickName);//修改昵称
		if(StringUtils.isNotBlank(sign))user.setSign(sign);//修改个人签名
		if(StringUtils.isNotBlank(headImg))user.setHeadImg(headImg);//修改头像
		if(StringUtils.isNotBlank(job))user.setJob(job);//修改职位
		if(StringUtils.isNotBlank(hobbies))user.setHobbies(hobbies);//修改兴趣
		if(StringUtils.isNotBlank(constellation))user.setConstellation(constellation);//修改星座
		if(NumberUtils.isValid(marriage))user.setMarriage(marriage);//修改情感状态
		boolean  b = userService.update(user);//将用户资料提交到数据库
		JsonObject json = new JsonObject();
		if(b)json.addProperty("code", 200); else json.addProperty("code", 500);
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}	
	
	
	/**
	 * 定位 和联系物业
	 * @param callback
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/estateInfo",produces = "application/json; charset=utf-8")
	public @ResponseBody String estateInfo(
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		User sessionuser=getSessionUser(session);
		if(sessionuser==null)throw new APICodeException(-4, "你还没登陆!");
		User user = userService.select(sessionuser.getId());//根据id查询用户返回用户信息
		JsonObject json = new JsonObject();
		json.addProperty("estateName", "华阳小区");
		json.addProperty("estateTel", "13355555555");
		if(user!=null){
			Estate e  = estateService.select(user.getEstateId());
			if(e!=null){
				json.addProperty("estateName", e.getName());
				json.addProperty("estateTel", e.getLinkTel());
			}
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}	
	/**
	 * 余额明细
	 * @param callback
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/balanceInfo",produces = "application/json; charset=utf-8")
	public @ResponseBody String list(@RequestParam(value="pager", required=false)Pager<Balance> pager, Map<Object, Object> model,
			@RequestParam(value = "type",required = false ) String type,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		if(pager==null){
			pager=new Pager<Balance>();
		}
		User sessionuser=getSessionUser(session);
		if(sessionuser==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		if(StringUtils.isNotBlank(type)){
			pager.getSearchMap().put("type", type);
		}else{
			throw new APICodeException(-9, "参数错误!");
		}
		pager.getSearchMap().put("userId", String.valueOf(sessionuser.getId()));
		pager=balanceService.getPager(pager);
		List<Balance> list=pager.getItemList();
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		JsonArray array=new JsonArray();
		json.add("arrays", array);
		for (Balance li : list) {
			//将整个对象的字段封装成json
			//余下的是把需要处理的字段处理后覆盖掉
			JsonObject jsonObj =new JsonObject();
					//gson.toJsonTree(li).getAsJsonObject();
			jsonObj.addProperty("name", li.getName());
			jsonObj.addProperty("money", li.getMoney());
			jsonObj.addProperty("time", TagUtils.formatDate(li.getCreateTime()));
			array.add(jsonObj);
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	/* 3.物业缴费 */
	@RequestMapping(value="/costList",produces = "application/json; charset=utf-8")
	public @ResponseBody String costInfo(@RequestParam(value="pager", required=false)Pager<PayMent> pager, Map<Object, Object> model,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		if(pager==null){
			pager=new Pager<PayMent>();
		}
		User sessionuser=getSessionUser(session);
		if(sessionuser==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		JsonObject json =new JsonObject();
		pager.getSearchMap().put("roomId", sessionuser.getRoomId().toString());
		payMentService.getPager(pager);
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<PayMent> list = pager.getItemList();
		JsonArray array=new JsonArray();
		json.add("arrays", array);
		Room r = roomService.select(sessionuser.getRoomId());
		if(r==null)throw new APICodeException(-9, "房间无效!");
		Estate e = estateService.select(r.getRelativeId());
		if(e==null)throw new APICodeException(-9, "小区无效!");
		City c = cityService.select(e.getCityCode());
		if(c==null)throw new APICodeException(-9, "城市无效!");
		StringBuffer sbf = new StringBuffer();
		sbf.append(c.getName()).append(e.getName()).append(r.getRoomZ()).append("幢").append(r.getRoomDy())
		.append("单元").append(r.getRoomNo()).append("室");
		for (PayMent li : list) {
			JsonObject jsonObj =new JsonObject();
					//gson.toJsonTree(li).getAsJsonObject();
			jsonObj.addProperty("room",sbf.toString());
			jsonObj.addProperty("id",li.getId());
			jsonObj.addProperty("price", li.getPrice());
			jsonObj.addProperty("startTime", li.getStartTime().substring(0,7));
			jsonObj.addProperty("endTime", li.getEndTime().substring(0,7));
			jsonObj.addProperty("createTime", TagUtils.formatDate(li.getCreateTime()).substring(0,10));
			jsonObj.addProperty("status", li.getStatus());
			array.add(jsonObj);
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}
	
	/* 4.车位查询  */

	/* 5.在线报修 */
	
	/* 6.银行卡提现 提现记录 */
	
	
}
